import os
import subprocess

from loguru import logger
from selenium import webdriver

from utils.set_cookie import SetCookies


class BaseDriver:

    def __init__(self, browser, ifRequest=True):
        self.ifRequest = ifRequest
        self.browser_driver(browser)

    def browser_driver(self, browser):
        try:
            if browser == 'chrome':
                try:
                    subprocess.run(["uname"])
                    runEnv = False
                except Exception as e:
                    runEnv = False
                if runEnv:
                    wch = webdriver.ChromeOptions()
                    wch.add_argument("ignore-certificate-errors")
                    # 下面2行chrome76及以上禁用chrome受自动软件控制
                    wch.add_experimental_option("useAutomationExtension", False)
                    wch.add_experimental_option("excludeSwitches", ["enable-automation"])
                    capabilities = {
                        "browserName": "chrome",
                        "version": "",
                        "platform": "ANY",
                        "javascriptEnabled": True,
                        "marionette": True,
                    }

                    self.driver = webdriver.Remote("http://11.164.2.56:5001/wd/hub", desired_capabilities=capabilities,
                                                   options=wch)
                    if str(os.environ["ifCookie"]) == "1":
                        self.ifRequest = True
                    else:
                        self.ifRequest = False
                else:
                    chrome_options = webdriver.ChromeOptions()
                    prefs = {"": ""}
                    prefs["credentials_enable_service"] = False
                    prefs["profile.password_manager_enabled"] = False
                    chrome_options.add_experimental_option("prefs", prefs)
                    chrome_options.add_argument("ignore-certificate-errors")
                    # 下面2行chrome76及以上禁用chrome受自动软件控制
                    chrome_options.add_experimental_option("useAutomationExtension", False)
                    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
                    # chrome_options=chrome_options
                    self.driver = webdriver.Chrome(executable_path="/Users/anxiaodong/IdeaProjects/java_auto/src/main/resources/chromedriver",chrome_options=chrome_options)

            elif browser == 'firefox':
                self.driver = webdriver.Firefox()
            else:
                self.driver = webdriver.Edge()

            print("self.ifRequest"+str(self.ifRequest))
            if self.ifRequest:
                self.request_login()
            else:
                self.login()
        except Exception as e:
            logger.error(e)

    def first_start(self):
        try:
            # 用一个循环状态来控制是否登录成功
            loop_status = True
            while loop_status:
                # 检验cookies是否生效
                login_status = SetCookies(self.driver).check_cookies()
                if login_status:
                    loop_status = False
                else:
                    self.request_login()
        except Exception as e:
            print(e)

    def request_login(self):
        self.driver.get("https://www.baidu.com")

    def login(self):
        ...